local hx__minga = fk.CreateSkill {
  name = "hx__minga",
}

hx__minga:addEffect(fk.AfterCardsMove, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:usedEffectTimes(self.name) > player.maxHp then return false end
    if player:hasSkill(hx__minga.name) then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Player.Hand and move.skillName ~= hx__minga.name and move.moveReason == fk.ReasonDraw then
          return true
        end
      end
      for _, move in ipairs(data) do
        if move.from == player and move.skillName ~= hx__minga.name then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, hx__minga.name)
    local pd_targets = table.filter(room:getOtherPlayers(player), function (p) return player:canPindian(p) and p:getMark("@@hx__minga-round") == 0 end)
    if not player:isKongcheng() and #pd_targets > 0 then
      local tos = room:askToChoosePlayers(player, {
        targets = pd_targets,
        min_num = 1,
        max_num = player.maxHp,
        skill_name = hx__minga.name,
        prompt = "请选择至多%arg名其他角色与他们同时拼点:::"..player.maxHp,
      })
      if #tos > 0 then
        room:sortByAction(tos)
        local pindian = player:pindian(tos, hx__minga.name)
        for _,p in ipairs(tos) do
          if pindian.results[p].winner then
            if pindian.results[p].winner == player then
              room:addPlayerMark(p, "@@hx__minga-round")
            else
              room:addPlayerMark(player, "@@hx__minga-round")
            end
          end
        end
      end
    end
  end,
})

hx__minga:addEffect(fk.BeforeDrawCard, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__minga.name) and target:getMark("@@hx__minga-round") ~= 0
  end,
  on_trigger = function(self, event, target, player, data)
    while data.num > 2 do
      if not player:hasSkill(hx__minga.name) then return end
      self:doCost(event, target, player, data)
    end
  end,
  on_use = function (self, event, target, player, data)
    data.num = 1
  end,
})

return hx__minga